package com.wkbb.basic.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;


/**
 * @author dengjingyuan
 * @date 2019-07-04 10:32
 */
@Slf4j
public class WebankUtil {

    /**
     * 请求成功
     */
    public static final String CODE_SUCCESS = "0";
    /**
     * 找不到用户
     */
    public static final String CODE_NOT_FOUND_PERSON = "34891101";
    /**
     * 用户已存在
     */
    public static final String PERSON_ALREADY_EIXSTS = "34891109";
    /**
     * 设备已存在
     */
    public static final String DEVICE_ALREADY_EXISTS = "34891301";
    /**
     * 用户组不存在
     */
    public static final String GROUP_NOT_EXISTS = "34891111";

    /**
     * 未找到人脸
     */
    public static final String ERROR_FACE = "-1101";

    /**
     * 非本人人脸
     */
    public static final String ERROR_MFACE = "-1604";

    /**
     * Token过期
     */
    public static final String TOKEN_TIME_OUT = "100102";
    /**
     * 获取 Access Token
     */
    public static final String ACCESS_TOKEN = "/iotai-openapi/oauth2/access_token";
    /**
     * 获取 SIGN ticket
     */
    public static final String API_TICKET = "/iotai-openapi/oauth2/api_ticket";
    /**
     * 新增用户组信息
     */
    public static final String ADD_GROUP = "/iotai-openapi/partner/group/addGroup";
    /**
     * 修改用户组信息
     */
    public static final String MODIFY_GROUP = "/iotai-openapi/partner/group/modifyGroup";
    /**
     * 删除用户组信息
     */
    public static final String DEL_GROUP = "/iotai-openapi/partner/group/delGroup";
    /**
     * 新增用户信息
     */
    public static final String ADD_PERSON = "/iotai-openapi/partner/person/addPerson";
    /**
     * 修改用户信息
     */
    public static final String MODIFY_PERSON = "/iotai-openapi/partner/person/modifyPerson";
    /**
     * 删除用户信息
     */
    public static final String DEL_PERSON = "/iotai-openapi/partner/person/delPerson";
    /**
     * 设置人员关联组
     */
    public static final String SET_GROUP = "/iotai-openapi/partner/person/setGroups";
    /**
     * 注册设备信息
     */
    public static final String ADD_DEVICE = "/iotai-openapi/partner/device/addDevice";
    /**
     * 修改设备信息
     */
    public static final String MODIFY_DEVICE = "/iotai-openapi/partner/device/modifyDevice";
    /**
     * 删除设备信息
     */
    public static final String DEL_DEVICE = "/iotai-openapi/partner/device/delDevice";
    /**
     * 获取设备信息
     */
    public static final String GET_DEVICE = "/iotai-openapi/partner/device/getProperty";
    /**
     * 1:N 在线人脸检索
     */
    public static final String SEARCH_FACE = "/iotai-openapi/file/searchFace";
    /**
     * 申请appid
     */
    public static final String APPLY_APP = "/iotai-openapi/app/applyApp";
    /**
     * 查询appid申请状态
     */
    public static final String SEARCH_APP = "/iotai-openapi/app/queryApp";

    /**
     * 新增用户信息
     */
    public static final String ADD_PERSON_BY_TCTHIRDSERVICE = "/tcthird/v1/iotapi/addWeBankUserFace";
    /**
     * 修改用户信息
     */
    public static final String MODIFY_PERSON_BY_TCTHIRDSERVICE = "/tcthird/v1/iotapi/modifyWeBankUserFace";


    /**
     * 解析是否请求成功
     *
     * @param result 请求结果
     * @return 请求成功的JsonObject，不成功返回null
     */
    public static JSONObject getJsonObjectIfSuccess(String result) {
        if (StringUtil.isEmpty(result)) {
            return null;
        }

        JSONObject jsonObject = JSON.parseObject(result);
        String code = jsonObject.getString("code");
        if (StringUtil.isNullOrBlank(code)) {
            return null;
        }

        if (CODE_SUCCESS.equals(code)) {
            return jsonObject;
        }

        return null;
    }

    /**
     * 获取message
     *
     * @param result 请求结果
     * @return 请求是否成功
     */
    public static String getMessage(String result) {
        if (StringUtil.isEmpty(result)) {
            return "出错";
        }

        JSONObject jsonObject = JSON.parseObject(result);
        String msg = jsonObject.getString("msg");
        if (msg == null) {
            return "失败";
        }

        return msg;
    }

    /**
     * 获取code
     *
     * @param result 请求结果
     * @return 请求是否成功
     */
    public static String getCode(String result) {
        if (StringUtil.isEmpty(result)) {
            return null;
        }

        JSONObject jsonObject = JSON.parseObject(result);
        return jsonObject.getString("code");
    }

    /**
     * 解析是否请求成功
     *
     * @param result 请求结果
     * @return 请求是否成功
     */
    public static boolean checkIfSuccess(String result) {
        if (StringUtil.isEmpty(result)) {
            return false;
        }

        JSONObject jsonObject = JSON.parseObject(result);
        String code = jsonObject.getString("code");
        if (StringUtil.isNullOrBlank(code)) {
            return false;
        }

        return CODE_SUCCESS.equals(code);
    }

    public static boolean personAlreadyExists(String webankResult) {
        return PERSON_ALREADY_EIXSTS.equals(getCode(webankResult));
    }

    public static boolean deviceAlreadyExists(String webankResult) {
        return DEVICE_ALREADY_EXISTS.equals(getCode(webankResult));
    }

    public static boolean groupNotExists(String webankResult) {
        return GROUP_NOT_EXISTS.equals(getCode(webankResult));
    }

    public static boolean userNotExists(String webankResult) {
        return CODE_NOT_FOUND_PERSON.equals(getCode(webankResult));
    }

    public static boolean userNotBindExists(String webankResult) {
        return PERSON_ALREADY_EIXSTS.equals(getCode(webankResult));
    }

    public static boolean userNotMyFaceExists(String webankResult) {
        return ERROR_MFACE.equals(getCode(webankResult));
    }

    public static ResultDTO<Object> returnWebankResult(String result) {
        if (null == result) {
            return new ResultDTO<>(ErrorCode.ERRROR_WEBANKAPPID);
        }else if(WebankUtil.userNotExists(result)){
            return new ResultDTO<>(ErrorCode.PERSONIDNOTEXIST);
        }else if(WebankUtil.groupNotExists(result)){
            return new ResultDTO<>(ErrorCode.ERRROR_NULL_GROUP);
        }else if(WebankUtil.userNotMyFaceExists(result)){
            return new ResultDTO<>(ErrorCode.CHECKFACE_2120020);
        }

        if (!webankCheckResult(result)) {
            return new ResultDTO<>(ErrorCode.CHECKFACEERROR);
        }

        return new ResultDTO<>();
    }

    public static Boolean webankCheckResult(String result){
        if(JSON.parseObject(result).getString(YouTuUtil.CODE).equals(YouTuUtil.CODE_SUCCESS)){

            return true;
        }
        return false;
    }

}
